#include "DeviceSwitch.h"
#include "common.h"
#include "ModbusCRC16.h"

using namespace std;

DeviceSwitch::~DeviceSwitch() {
    close(m_fd);
    m_fd = -1;
}


void DeviceSwitch::func(int& data) {
    cout << "switch:" << data << endl;
    unsigned char crcLo, crcHi;

    unsigned char buf[8] = {0x01, 0x06, 0x00, (unsigned char)data, 0x00, 0x04, 0xF8, 0x08};

    GetModbusCRC16(buf, 6, &crcLo, &crcHi);

    buf[6] = crcLo;
    buf[7] = crcHi;

    set_uart(m_fd, 38400, 8, 'N', 1);

    printf("send message is \n");
    for(int i = 0;i < 8;i++){
        printf("0x%02X ",buf[i]);
    }
    printf("\n");

    write(m_fd, buf, 8);

    sleep(1);

    int count = read(m_fd, buf, 8);
    
    printf("read %d message2 is \n",count);
    for(int i = 0;i < 8;i++){
        printf("0x%02X ",buf[i]);
    }
    printf("\n");
}